######该字段需要用到四个值参与计算
from typing import List

from app.services.business.gaolu_diagnose.ind.abstract_ind import AbstractInd
import logging
from app.exts import ironman_redis as redis


class CG_LT_GL_GL04_KTP(AbstractInd):

    def get_name(self):
        return "CG_LT_GL_GL04_KTP"

    def get_data_source(self):
        return "mysql"

    def get_sql(self, params) -> list:
        sql_TSWD = """    
                    SELECT
        	        syn_tq_rgaoluchutie.ftswd AS CG_LT_GL_GL04_TSWD,
        	        syn_tq_rgaoluchutie.fdatetime
                        FROM
        	        syn_tq_rgaoluchutie
                    WHERE
                    syn_tq_rgaoluchutie.ftswd NOT LIKE '%#%'
                    and syn_tq_rgaoluchutie.fdatetime >= '{dt_start}' 
                    and syn_tq_rgaoluchutie.fdatetime <= '{dt_end}' 
                    order by syn_tq_rgaoluchutie.fdatetime desc
                        """.format(**params)

        sql_Tie_SiTi = """      
                    select round(avg(jyh.jyjg),3) as CG_LT_GL_GL04_Tie_V from 
                     (select jyh.*,jyt.ph 
                    from syn_zl_zl_jyh as jyh ,syn_zl_zl_jyt as jyt , syn_zl_zl_tqpd as tqpd 
                     where  jyh.jcxmbm='01058' and jyh.zfbz='0' 
                           and SUBSTRING(jyt.ph, 3, 1) = '4' 
                   and  jyt.jytid = jyh.jytid 
                    and jyh.jyjg NOT LIKE '%#%' 
                   and  tqpd.tjsj >= '{dt_start}' 
                   and tqpd.tjsj <= '{dt_end}' ) as jyh
                   """.format(**params)

        redis.set('sql_TSWD', sql_TSWD)
        redis.set('sql_Tie_SiTi', sql_Tie_SiTi)
        return [sql_TSWD, sql_Tie_SiTi]

    @AbstractInd.cache_wrapper
    def get_result(self):
        sql_list: List[str] = self.get_sql(self.params)
        cursor = self.mysql_cursor
        #####字段1
        tswd_sql = sql_list[0]
        logging.info(tswd_sql)
        cursor.execute(tswd_sql)
        result = cursor.fetchone()
        TSWD = float(result['CG_LT_GL_GL04_TSWD']) if result is not None \
                                                      and result['CG_LT_GL_GL04_TSWD'] is not None \
            else 0
        #####字段2
        siti_sql = sql_list[1]
        logging.info(siti_sql)
        cursor.execute(siti_sql)
        result = cursor.fetchone()
        SiTi = float(result['CG_LT_GL_GL04_Tie_V']) if result is not None \
                                                       and result['CG_LT_GL_GL04_Tie_V'] is not None \
            else 0

        return round(((TSWD - 1400) / 100) * ((TSWD - 1400) / 100) / SiTi, 2)
